<?php
	session_start();

	function verifyUser(){

		$autenticated;

		if(isset($_SESSION['usr']) && isset($_SESSION['pswd'])){
			$autenticated = true;
		}
		else{
			$autenticated = false;
		}
		return $autenticated;
	}

	function f000($sql){
		$autAction = $_GET['autAction'];
		$userName = $_GET['userName'];
		$password = $_GET['password'];

		if($autAction == "logon" && $userName != "" && $password != ""){
			logOn($sql, $userName, $password);
		}
		elseif($autAction == "logout"){
			logOut();
		}
		elseif($autAction == "getUserData"){
			getUserData();
		}
		else{
			$temp = array();
			$temp['error'] = "000: Bad request";
			print(json_encode($temp));
		}
		exit();
	}

	function logOn($sql, $userName, $password){

		$queryString = 	
					"
					SELECT 
					group_id 
					FROM usr_Users 
					WHERE login = '".$userName."' AND pwd = '".$password."'
					";

		$result = $sql->query($queryString);

		if($result != NULL){

			$groupID = $result[0]['group_id'];

			if($groupID != null){
				$_SESSION['usr'] = $userName;
				$_SESSION['pswd'] = $password;
				$_SESSION['group'] = $groupID;
				echo "success";
			}
			else{
				$temp = array();
				$temp['error'] = "fail";
				print(json_encode($temp));
			}
		}
	}

	function logOut(){
		session_destroy();
		$temp = array();
		$temp['error'] = "logged_out";
		print(json_encode($temp));
	}

	function getUserData(){
		if($_SESSION['usr'] != "")
			echo $_SESSION['usr'];
		else{
			$temp = array();
			$temp['error'] = "No User";
			print(json_encode($temp));
		}
	}
?>